<template>
  <div>
    <el-row :gutter="20">
      <!--      机构信息-->
      <el-col :span="6">
        <el-card class="box-card" style="height: 800px">
          <el-tree :data="list" :props="defaultProps" @node-click="handleNodeClick" ></el-tree>
        </el-card>
      </el-col>
      <!--      新增机构提示-->
      <el-col :span="18" v-if="orgId == ''">
        <el-card class="box-card" style="height: 800px">
          <el-col :sm="12" :lg="6">
            <el-result icon="warning" title="警告提示" subTitle="请选择机构">
              <template slot="extra">
              </template>
            </el-result>
          </el-col>
        </el-card>
      </el-col>
      <!--      添加定时任务-->
      <el-col :span="18" v-if="orgId != '' && updJobData == null && updJobData != ''">
        <el-card class="box-card" style="height: 800px">
          <h3>新增</h3>
          <el-divider></el-divider>
          <el-form ref="addJobData" :model="addJobData" label-width="150px">
            <el-form-item label="机构名称:" prop="orgName">
              <el-input type="text" disabled v-model="addJobData.orgName"></el-input>
            </el-form-item>
            <el-form-item label="任务名称:" prop="beanName">
              <el-input type="text" v-model="addJobData.beanName"></el-input>
            </el-form-item>
            <el-form-item label="* CRON表达式:" prop="cronExpression">
              <el-input type="text" v-model="addJobData.cronExpression"></el-input>
            </el-form-item>
            <el-form-item label="参数:" prop="params">
              <el-select v-model="addJobData.params" clearable placeholder="请选择">
                <el-option value="1" label="距离最少"></el-option>
                <el-option value="2" label="成本最少"></el-option>
                <el-option value="3" label="时间最少"></el-option>
                <el-option value="4" label="中转最少"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="addJobData.status">
                <el-radio :label="0" >停止</el-radio>
                <el-radio :label="1" >正常</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input type="text" placeholder="请输入备注" v-model="addJobData.remark"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="addJob()" :disabled="tianjiabut" type="primary" size="medium">增加</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
      <!--      修改定时任务-->
      <el-col :span="18" v-if="orgId != '' && updJobData != null && updJobData != ''">
        <el-card class="box-card" style="height: 800px">
          <h3>修改</h3>
          <el-divider></el-divider>
          <el-form ref="updJobData" :model="updJobData" label-width="150px">
            <el-form-item label="机构名称:" prop="orgName">
              <el-input type="text" disabled v-model="updJobData.orgName"></el-input>
            </el-form-item>
            <el-form-item label="* 任务名称:" prop="beanName">
              <el-input type="text" placeholder="请输入任务名称" v-model="updJobData.beanName"></el-input>
            </el-form-item>
            <el-form-item label="* CRON表达式:" prop="cronExpression">
              <el-input type="text" placeholder="请输入表达式" v-model="updJobData.cronExpression"></el-input>
            </el-form-item>
            <el-form-item label="参数:" prop="params">
              <el-select v-model="updJobData.params" clearable placeholder="请选择">
                <el-option value="1" label="距离最少"></el-option>
                <el-option value="2" label="成本最少"></el-option>
                <el-option value="3" label="时间最少"></el-option>
                <el-option value="4" label="中转最少"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="updJobData.status">
                <el-radio :label="0" >停止</el-radio>
                <el-radio :label="1" >正常</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input type="text" placeholder="请输入备注" v-model="updJobData.remark"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="updJob()" :disabled="xiugaibut" type="primary" size="medium">修改</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import os from '../../../../src/api/logisticsmanagement/organization/organizationScope'
import method from '../../../api/logisticsmanagement/schedulingmanagement/intelligentScheduling'

export default {
  name: "intelligentScheduling",
  data() {
    return{
      xiugaibut: false,
      tianjiabut: false,
      //机构树
      list:[],
      // 设置树，名字，子集合
      defaultProps: {
        children: 'orgTreeDtoList',
        label: 'name'
      },
      // 查询树的条件
      data:{},
      // 你选择的机构的信息
      setLatLng:{
        id: ""
      },
      //  当前机构ID
      orgId: "",
      //  给机构添加定时任务的数据
      addJobData: {},
      //  给机构修改定时任务的数据
      updJobData: {},
      upd: {},
    }
  },
  methods: {
    //  确定添加机构的定时任务
    addJob() {
      this.tianjiabut = true
      let dx = {
        beanName: this.addJobData.beanName,
        params: this.addJobData.params,
        cronExpression: this.addJobData.cronExpression,
        status: this.addJobData.status,
        remark: this.addJobData.remark,
        businessId: this.addJobData.businessId
      }

      method.insJob(dx).then(({data}) => {
        if(data.code == 200){
          this.$notify({
            title: '成功',
            message: '新增成功!',
            type: 'success'
          })
          this.handleNodeClick(this.upd)
        }
        this.tianjiabut = true
      })
    },
    //  确定修改机构的定时任务
    updJob() {
      this.xiugaibut = true
      let dx = {
        "id": this.updJobData.id,
        "beanName": this.updJobData.beanName,
        "params": this.updJobData.params,
        "cronExpression": this.updJobData.cronExpression,
        "status": this.updJobData.status,
        "remark": this.updJobData.remark
      }
      method.updJob(dx).then(({data}) => {
        if(data.code == 200){
          this.$notify({
            title: '成功',
            message: '修改成功!',
            type: 'success'
          })
        }
        this.xiugaibut = false
      })
    },
    // 点击机构触发
    handleNodeClick(obj) {
      this.upd = obj
      this.$store.commit("startAnimation",true);
      this.orgId = obj.pdCoreOrg.id
      method.findJobByOrgId(obj.pdCoreOrg.id).then(({data}) => {
        if(data.code == 200){
          if(data.data != null && data.data != ""){
            this.$set(data.data,'orgId',obj.pdCoreOrg.id)
            this.$set(data.data,'orgName',obj.pdCoreOrg.name)
            this.updJobData = data.data
            this.$store.commit("startAnimation",false);
          }else{
            this.updJobData = null
            this.addJobData = {
              beanName:"",
              params:"",
              cronExpression:"",
              status:"",
              remark:"",
              businessId:obj.pdCoreOrg.id,
              orgName: obj.pdCoreOrg.name
            }
            this.$store.commit("startAnimation",false);
          }
        }
      })
    },
    // 递归设置机构名称
    dg (data) {
      if(data!=null){
        for (var i = 0; i < data.length; i++) {
          if (data[i].orgTreeDtoList.length > 0) {
            this.dg(data[i].orgTreeDtoList)
          }
          this.$set(data[i], 'name', data[i].pdCoreOrg.name)
        }
      }
    },
    // 获取机构树
    getTree(){
      os.queryCoreOrgAllInfo(this.data).then(({data})=>{
        this.dg (data.data)
        this.list = data.data
      })
    },
  },
  created() {
    this.getTree()
  }
}
</script>
<style>
  .map {
    width: 100%;
    height: 550px;
  }
</style>
